package top.carrotguo.weibo.ui.base;

import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import butterknife.BindView;
import butterknife.OnClick;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.adapter.ImagePagerAdapter;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.utils.FileUtil;

public class ImageActivity extends BaseActivity{
    @BindView(R.id.vp_image)
    ViewPager ivImage;
    @BindView(R.id.tv_page)
    TextView tvPage;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.btn_origin)
    Button btnOrigin;

    public static final String IMAGES = "IMAGE";
    public static final String CUR_INDEX = "CUR_INDEX";
    private ArrayList<HomeTimeLine.UsersBean.Pic> mDatas;
    private ImagePagerAdapter mAdapter;
    private int mCurIndex = 0;
    private ExecutorService cachePool;

    @Override
    public int provideContentViewId() {
        return R.layout.activity_image;
    }

    @Override
    public Bundle getBundleExtras(Bundle bundle) {
        mDatas = (ArrayList<HomeTimeLine.UsersBean.Pic>) bundle.getSerializable(IMAGES);
        mCurIndex = bundle.getInt(CUR_INDEX);
        return null;
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new ImagePagerAdapter(mContext,mDatas);
        ivImage.setAdapter(mAdapter);
        ivImage.setCurrentItem(mCurIndex);
        tvPage.setText((mCurIndex+1)+"  /  "+mDatas.size());
        ivImage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                mCurIndex = i;
                tvPage.setText((i+1)+"  /  "+mDatas.size());
                if (mDatas.get(i).isSave()) {
                    btnSave.setVisibility(View.GONE);
                } else {
                    btnSave.setVisibility(View.VISIBLE);
                }
                if (mDatas.get(i).isHasLarge()) {
                    btnOrigin.setVisibility(View.GONE);
                } else {
                    btnOrigin.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @OnClick(R.id.btn_save)
    public void save(View view){
        saveFile(mCurIndex);
    }

    private void saveFile(int position){
        Logger.e("开始保存图片");
        //保存图片
        String path = Environment.getExternalStorageDirectory()
                +"/"+"weibodemo"
                +"/"+"image";
        String name = "weibo_pic_"+SystemClock.currentThreadTimeMillis()+".jpg";

        //创建缓存池
        if (cachePool == null) {
            cachePool = Executors.newCachedThreadPool();
        }
        cachePool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    File sourceFile = Glide.with(mContext).asFile()
                            .load(mDatas.get(position).getLarge_pic()).submit().get();
                    File newFile = new File(path,name);
                    Logger.e("path："+newFile.getAbsolutePath());
                    if (FileUtil.copyFile(sourceFile,newFile)) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                btnSave.setVisibility(View.INVISIBLE);
                                mDatas.get(mCurIndex).setSave(true);
                                showToast("保存成功");
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("保存失败");
                            }
                        });
                    }
                } catch (ExecutionException | InterruptedException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("保存失败");
                        }
                    });
                    Logger.e("保存失败");
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick(R.id.btn_origin)
    public void showOrigin(View view){
        btnOrigin.setVisibility(View.GONE);
        mAdapter.showLargeImage(mCurIndex);
    }

}
